﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Tool_System_Client
{
    #region <<--加密算法 DESEncrypt
    public class DESEncrypt
    {
        public const string m_sCompanyName = "武汉铁路局襄阳机务段";
        public DESEncrypt()
        { }
        #region =========加密===========
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Encrypt(string Text)
        {
            return Encrypt(Text, m_sCompanyName);
        }

        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string Encrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray;
            inputByteArray = Encoding.Default.GetBytes(Text);
            des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }
        #endregion
        #region =======解密==================
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public string Decrypt(string Text)
        {
            return Decrypt(Text, m_sCompanyName);
        }
        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string Decrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            int len;
            len = Text.Length / 2;
            byte[] inputByteArray = new byte[len];
            int x, i;
            for (x = 0; x < len; x++)
            {
                i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }
        #endregion
        #region
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <returns></returns>
        public string md5(string str)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
        }
        #endregion
    }
    #endregion

    public class Userinfo
    {
        //员工标示号
        private Boolean m_bLogin = false;
        //员工标示号
        private int m_iID = 0;
        //登录工号
        private string m_sUserCode = string.Empty;
        private string m_sUserName = string.Empty;

        public bool BLogin { get => m_bLogin; set => m_bLogin = value; }
        public int IID { get => m_iID; set => m_iID = value; }
        public string SUserCode { get => m_sUserCode; set => m_sUserCode = value; }
        public string SUserName { get => m_sUserName; set => m_sUserName = value; }

        public void Clear()
        {
            m_bLogin = false;
            m_iID = 0;
            m_sUserCode = string.Empty;
            m_sUserName = string.Empty;
        }
    }

    class DataCache
    {
        public static Color colorPanelBack = Color.FromArgb(58, 166, 101);
        public static Color colorPanelSold = Color.FromArgb(158, 158, 158);
        public static Color colorCaptionReady = Color.FromArgb(193, 222, 204);
        public static Color colorCaptionSold = Color.FromArgb(219, 219, 219);

        private static DataCache instance;
        private static object _lock = new object();

        private Userinfo m_oUserInfo = new Userinfo();

        public Userinfo OUserInfo { get => m_oUserInfo; set => m_oUserInfo = value; }

        public static DataCache GetInstance()
        {
            if (instance == null)
            {
                lock (_lock)
                {
                    if (instance == null)
                    {
                        instance = new DataCache();
                    }
                }
            }
            return instance;
        }
    }


}
